//
//  HXLOverseasView.m
//  On The Load
//
//  Created by 晓琳 on 15/11/8.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "HXLOverseasView.h"
#import "BaseTableView.h"
#import "DataHandle.h"
#import "HXLCityModel.h"
#import "HXLLeavesModel.h"

@interface HXLOverseasView ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) BaseTableView *overseasTV;
@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation HXLOverseasView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void) createSubviews{
    self.overseasTV = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    self.overseasTV.delegate = self;
    self.overseasTV.dataSource = self;
    [self addSubview:self.overseasTV];
    
    self.dataArray = [[DataHandle shareDataHandle].finDic objectForKey:@"areas2"];
    self.titleArray = [NSMutableArray array];
    for (HXLCityModel *model in self.dataArray) {
        [self.titleArray addObject:model.name];

    }
    
    
    
}
//分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.titleArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    HXLCityModel *model = [self.dataArray objectAtIndex:section];
    return model.leavesArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifer = @"overseasCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:cellIdentifer];
    }
    HXLCityModel *model = [self.dataArray objectAtIndex:indexPath.section];
    HXLLeavesModel *smallModel = [model.leavesArray objectAtIndex:indexPath.row];
//    NSLog(@"xxxxxxxxx = %@",smallModel.countryId);
    cell.textLabel.text = smallModel.cityName;
    
    return cell;
    
}
//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.overseasTV deselectRowAtIndexPath:indexPath animated:YES];
    HXLCityModel *model = [self.dataArray objectAtIndex:indexPath.section];
    HXLLeavesModel *smallModel = [model.leavesArray objectAtIndex:indexPath.row];
    
    [self.myDelegate popPlanviewController:smallModel];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.titleArray objectAtIndex:section];
    
    
}

//检索字母
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.titleArray;
}

@end
